import { IFeidaoAiBrowserComponent } from '@feidao-factory/atom-web/interfaces';
import error from '@feidao-factory/atom-web/msgbox/error';
import client_login from '@feidao-factory/atom-web/msg/user-login';
import storage_put from '@feidao-factory/atom-web/storage/put';

export default async function a004(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
	const user_name = fd.data.node.querySelector('[data-feidao-id="user_name"]') as HTMLInputElement;
	if (!user_name.value) {
		return error('请输入用户名');
	}
	const user_pwd = fd.data.node.querySelector('[data-feidao-id="user_pwd"]') as HTMLInputElement;
	if (!user_pwd.value) {
		return error('请输入密码');
	}
	try {
		// 调用登录的通用服务
		const res = await client_login(user_name.value, user_pwd.value);
		await storage_put('user', res);
		await storage_put('user_code', res.usercode);
		await storage_put('user_name', res.user_name);
		// 跳转到其他页面
		const backurl = fd.data.params.backurl as string;
		if (backurl) {
			window.location.href = backurl;
		} else {
			window.location.href = 'home.html';
		}
	} catch (e) {
		return error((e as Error).message);
	}
}
